package com.fanghuaman.common.exception;

import com.fanghuaman.model.common.vo.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.WebRequest;

import java.util.Date;

/**
 * @author shiminghui
 * @date 2024/12/22 15:14
 * @description: 全局异常处理器
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    // 处理特定异常
    @ExceptionHandler(value = {FanghuamanException.class})
    public ResponseEntity<Object> exception(FanghuamanException e) {
        ResponseResult<Object> response = ResponseResult.fail(e.getEmus());
        return new ResponseEntity<>(response, HttpStatus.OK); // 500
    }

    // 处理所有其他异常
    @ExceptionHandler(value = {Exception.class})
    public ResponseEntity<Object> exception(Exception e) {
        log.error("服务器异常", e);
        ResponseResult<Object> result = ResponseResult.fail("服务器异常");
        return new ResponseEntity<>(result, HttpStatus.INTERNAL_SERVER_ERROR); // 500
    }
}




